package com.yangle.frame.admin.service;

import com.yangle.frame.admin.entity.FrameDatabase;
import com.yangle.frame.admin.entity.FrameMenu;
import com.yangle.frame.admin.mapper.FrameDatabaseMapper;
import com.yangle.frame.admin.mapper.FrameMenuMapper;
import com.yangle.frame.common.service.BaseService;
import com.yangle.frame.common.utils.MapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
 * Created by yangle on 2018/5/27.
 */
@Service
public class FrameDataBaseService implements BaseService<FrameDatabase> {
    @Autowired
    private FrameDatabaseMapper frameDatabaseMapper;
    @Value("${com.yangle.database}")
    public String databasename;
    public List<FrameDatabase> getAll(FrameDatabase frameDatabase) {
        Example example = MapperUtils.getExampleByClass(FrameDatabase.class, frameDatabase);
        return frameDatabaseMapper.selectByExample(example);
    }

    public void create(FrameDatabase frameDatabase) {
        frameDatabaseMapper.insert(frameDatabase);
    }

    public void update(FrameDatabase frameDatabase) {
        frameDatabaseMapper.updateByPrimaryKey(frameDatabase);
    }

    public FrameDatabase select(String id) {
        return frameDatabaseMapper.selectByPrimaryKey(id);
    }

    public void delete(String ids) {
        frameDatabaseMapper.deletes(Arrays.asList(ids.split(",")));
    }

    public void createOrAlterTable(FrameDatabase database){
        //判断id是否为空，如果为空则进行表的修改
        if(StringUtil.isNotEmpty(database.getId())){//不为空则说明是修改表的操作
            //获取原有表的名称
            FrameDatabase preTable = frameDatabaseMapper.selectByPrimaryKey(database.getId());
            String preTableName = preTable.getTableSqlName();
            String tableName=database.getTableSqlName();
            if(!preTableName.equals(tableName)){
                //执行表的修改操作
                //①查询表是否存在
                int i = frameDatabaseMapper.queryTableIsExists(databasename, preTableName);
                if(i>0){//说明存在，执行表名的修改操作
                    frameDatabaseMapper.alterTableName(preTableName,tableName);
                }else{
                    frameDatabaseMapper.createTable(database.getTableSqlName());
                }
            }
        }else{//新创建
            //①查询表是否存在
            int i = frameDatabaseMapper.queryTableIsExists(databasename, database.getTableSqlName());
            if(i==0){//不存在才创建
                frameDatabaseMapper.createTable(database.getTableSqlName());
            }
        }
    }
    /**
     * 删除表
     */
    public void dropTable(String ids){
for(String id:ids.split(",")){
        FrameDatabase preTable = frameDatabaseMapper.selectByPrimaryKey(id);
        frameDatabaseMapper.dropTable(preTable.getTableSqlName());}
    }
}
